const formatTime = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();

  return `${[year, month, day].map(formatNumber).join("/")} ${[
    hour,
    minute,
    second,
  ]
    .map(formatNumber)
    .join(":")}`;
};

const formatNumber = (n) => {
  n = n.toString();
  return n[1] ? n : `0${n}`;
};
function extractNumberFromString(str) {
  // 使用正则表达式匹配数字部分
  const match = str.match(/\d+/);
  if (str == "0.5元/个") return 0.5;
  if (match) {
    // 将匹配到的数字字符串转换为整型
    return parseInt(match[0], 10);
  }
  return null; // 如果没有匹配到数字，返回 null 或者其他默认值
}
function extractAndConvertChineseNumbers(str) {
  const chineseNumbers = [];
  const chineseNumberPattern = /零|一|二|三|四|五|六|七|八|九|十|百|千|万|亿/g;

  // 提取所有大写数字
  let match;
  while ((match = chineseNumberPattern.exec(str)) !== null) {
    chineseNumbers.push(match[0]);
  }

  // 将大写数字转换为阿拉伯数字
  const arabicNumbers = chineseNumbers.map(
    (chineseNum) => chineseToArabicMap[chineseNum]
  );

  return arabicNumbers;
}
function formatTime2(date) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();

  const ampm = hours >= 12 ? "下午" : "上午";
  const formattedHours = hours % 12 || 12;

  return `${year}年-${month}月-${day}号-${ampm}${formattedHours}点${minutes}分`;
}
function formatDate3(date) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
}
const chineseToArabicMap = {
  零: 0,
  一: 1,
  二: 2,
  三: 3,
  四: 4,
  五: 5,
  六: 6,
  七: 7,
  八: 8,
  九: 9,
  十: 10,
  百: 100,
  千: 1000,
  万: 10000,
  亿: 100000000,
};
module.exports = {
  formatTime,
  formatTime2,
  formatDate3,
  extractNumberFromString,
  extractAndConvertChineseNumbers,
};
